<?php
/**
 * 用户控制器
 * Created by  你老板
 * User: 你老板
 * Date: 2018/11/10
 * Time: 下午 5:10
 */
namespace app\home\controller;
use think\Controller;
use think\Request;
use \think\Db;
use app\admin\lib\Tree;
use app\admin\lib\Detail;
class Selection extends Controller{
	public function _initialize() {
	}
 //首页查询品牌
public function home_class(){
	$db = Db::name('product_class');
	$list = $db->select();
	$li =Tree::findmenu($list);
	echo json_encode($li);
	// dump($li);
}

//查询车型
public function vehicle(){
	$db = Db::name('product');
	$list = $db->limit(7)->select();
	$arr = '';
	foreach ($list as $key => $value) {
    $arr .= $value['id'].',';
  }
  $datad['id']=  array('in',$arr);
	$index = controller('Index');
	$result =$index->productall($datad,0,10);



	echo json_encode($result);
	// dump($result);


}
//多选
public function duoxuan(){
	$db = Db::name('product');
	// 默认顺序条件开始------------------------------------------
		$data = [];
			$order = 'id asc';
		if (isset($_POST['shunxu'])) {
				$moren = $_POST['shunxu'];//默认顺序
				if($moren == '月供最低'){
						$order = 'supply asc';//月供最低
				}elseif ($moren == '销量最高') {
						$order = 'volume desc';//销量从大到小
				}elseif ($moren == '车价最高') {
						$order = 'guidance_price desc';//指导价从大到小
				}elseif ($moren == '车价最低') {
						$order = 'guidance_price asc';//指导价从小到大
				}elseif ($moren == '首付最低') {
						$order = 'payments asc';//首付最低
				}
				elseif ($moren == '新车上市') {
						$order = 'addteime desc';//新车上市
				}
			}else{
				$order = 'id desc';//销量从大到小
			}
	// 默认顺序条件结束------------------------------------------
	//查询品牌条件-----------------------------------------------
	if (isset($_POST['pinpai_id']) and $_POST['pinpai_id'] !='') {
		$data['uid'] = $_POST['pinpai_id'];
	}
	//查询品牌条件结束-----------------------------------------------
	//首付首付1万以内-----------------------------------------------
	if (isset($_POST['shoufu'])) {
		$shoufu['shoufu'] = $_POST['shoufu'];
		if($_POST['shoufu']=='首付1万以内'){
				$data['payments']  = ['<','10000'];
		}elseif($_POST['shoufu']=='1~2万'){
				$data['payments']  = [['>',10000],['<',20000]];
		}elseif ($_POST['shoufu']=='首付2~3万') {
				$data['payments']  = [['>',20000],['<',30000]];
		}elseif ($_POST['shoufu']=='首付3~4万') {
				$data['payments']  = [['>',30000],['<',40000]];
		}elseif ($_POST['shoufu']=='首付5万以上') {
				$data['payments']  = ['>','50000'];
		}elseif($_POST['shoufu']=='首付0～6万'){
				$data['payments']  = [['>',0],['<',60000]];
		}elseif ($_POST['shoufu']=='首付1～6万') {
			$data['payments']  = [['>',10000],['<',60000]];
		}elseif ($_POST['shoufu']=='首付2～6万') {
			$data['payments']  = [['>',20000],['<',60000]];
		}elseif ($_POST['shoufu']=='首付3～6万') {
			$data['payments']  = [['>',30000],['<',60000]];
		}elseif ($_POST['shoufu']=='首付4～6万') {
			$data['payments']  = [['>',40000],['<',60000]];
		}elseif ($_POST['shoufu']=='首付5～6万') {
			$data['payments']  = [['>',50000],['<',60000]];
		}elseif ($_POST['shoufu']=='首付0～5万') {
			$data['payments']  = [['>',0],['<',50000]];
		}elseif ($_POST['shoufu']=='首付1～5万') {
			$data['payments']  = [['>',10000],['<',50000]];
		}elseif ($_POST['shoufu']=='首付2～5万') {
			$data['payments']  = [['>',20000],['<',50000]];
		}elseif ($_POST['shoufu']=='首付3～5万') {
			$data['payments']  = [['>',30000],['<',50000]];
		}elseif ($_POST['shoufu']=='首付4～5万') {
			$data['payments']  = [['>',40000],['<',50000]];
		}elseif ($_POST['shoufu']=='首付0～4万') {
			$data['payments']  = [['>',0],['<',40000]];
		}elseif ($_POST['shoufu']=='首付1～4万') {
			$data['payments']  = [['>',10000],['<',40000]];
		}elseif ($_POST['shoufu']=='首付2～4万') {
			$data['payments']  = [['>',20000],['<',40000]];
		}elseif ($_POST['shoufu']=='首付3～4万') {
			$data['payments']  = [['>',30000],['<',40000]];
		}elseif ($_POST['shoufu']=='首付0～3万') {
			$data['payments']  = [['>',0],['<',30000]];
		}elseif ($_POST['shoufu']=='首付1～3万') {
			$data['payments']  = [['>',10000],['<',30000]];
		}elseif ($_POST['shoufu']=='首付2～3万') {
			$data['payments']  = [['>',20000],['<',30000]];
		}elseif ($_POST['shoufu']=='首付0～2万') {
			$data['payments']  = [['>',0],['<',20000]];
		}elseif ($_POST['shoufu']=='首付1～2万') {
			$data['payments']  = [['>',10000],['<',20000]];
		}
	}
	//首付结束-----------------------------------------------
	//月供----------------------------------------------
		if (isset($_POST['yuegongs'])) {
				if ($_POST['yuegongs']=='不限') {
						$data['supply']  = ['>','0'];
				}elseif ($_POST['yuegongs']=='月供2000以内') {
						$data['supply']  = ['<','0'];
				}elseif ($_POST['yuegongs']=='月供2000~3000元') {
						$data['supply']  = [['>',2000],['<',3000]];
				}elseif ($_POST['yuegongs']=='月供4000~5000元') {
						$data['supply']  = [['>',4000],['<',5000]];
				}elseif ($_POST['yuegongs']=='月供5000~6000元') {
						$data['supply']  = [['>',5000],['<',6000]];
				}elseif ($_POST['yuegongs']=='月供6000元以上') {
							$data['supply']  = ['>','6000'];
				}elseif ($_POST['yuegongs']=='月供0千~6千') {
							$data['supply']  = [['>',0],['<',6000]];
				}elseif ($_POST['yuegongs']=='月供1千~6千') {
							$data['supply']  = [['>',1000],['<',6000]];
				}elseif ($_POST['yuegongs']=='月供2千~6千') {
							$data['supply']  = [['>',2000],['<',6000]];
				}elseif ($_POST['yuegongs']=='月供3千~6千') {
							$data['supply']  = [['>',3000],['<',6000]];
				}elseif ($_POST['yuegongs']=='月供4千~6千') {
							$data['supply']  = [['>',4000],['<',6000]];
				}elseif ($_POST['yuegongs']=='月供5千~6千') {
							$data['supply']  = [['>',5000],['<',6000]];
				}elseif ($_POST['yuegongs']=='月供0千~5千') {
							$data['supply']  = [['>',0],['<',5000]];
				}elseif ($_POST['yuegongs']=='月供2千~5千') {
							$data['supply']  = [['>',1000],['<',5000]];
				}elseif ($_POST['yuegongs']=='月供2千~5千') {
							$data['supply']  = [['>',2000],['<',5000]];
				}elseif ($_POST['yuegongs']=='月供3千~5千') {
							$data['supply']  = [['>',3000],['<',5000]];
				}elseif ($_POST['yuegongs']=='月供4千~5千') {
							$data['supply']  = [['>',4000],['<',5000]];
				}elseif ($_POST['yuegongs']=='月供0千~4千') {
							$data['supply']  = [['>',0],['<',4000]];
				}elseif ($_POST['yuegongs']=='月供1千~4千') {
							$data['supply']  = [['>',1000],['<',4000]];
				}elseif ($_POST['yuegongs']=='月供2千~4千') {
							$data['supply']  = [['>',2000],['<',4000]];
				}elseif ($_POST['yuegongs']=='月供3千~5千') {
							$data['supply']  = [['>',3000],['<',4000]];
				}elseif ($_POST['yuegongs']=='月供0千~3千') {
							$data['supply']  = [['>',0],['<',3000]];
				}elseif ($_POST['yuegongs']=='月供1千~3千') {
							$data['supply']  = [['>',1000],['<',3000]];
				}elseif ($_POST['yuegongs']=='月供2千~3千') {
							$data['supply']  = [['>',2000],['<',3000]];
				}elseif ($_POST['yuegongs']=='月供0千~2千') {
							$data['supply']  = [['>',0],['<',2000]];
				}elseif ($_POST['yuegongs']=='月供1千~2千') {
							$data['supply']  = [['>',1000],['<',2000]];
				}
		}
	//月供结束-----------------------------------------------
	//区间价格
	if (isset($_POST['jiage'])) {
			if ($_POST['jiage'] == '10万以下') {
				$data['package_price']  = ['<',100000];
			}elseif ($_POST['jiage']=='10~20万') {
						$data['package_price']  = [['>',100000],['<',200000]];
			}elseif ($_POST['jiage']=='20~50万') {
						$data['package_price']  = [['>',200000],['<',500000]];
			}elseif ($_POST['jiage']=='50万以上') {
						$data['package_price']  = ['>',500000];
			}
	}
	// dump($data);die;
	$list = $db->limit(10)->where($data)->order($order)->select();
	$arr = [];
	foreach ($list as $key => $value) {

		$pic = json_decode($value['pic'],true);
		$dow_payments = json_decode($value['dow_payments'],true);

		$arr[] = [
				'id'=>$value['id'],
				'title'=>$value['title'],
				'name'=>$value['name'],
				'payments'=>round($value['payments']/10000,2),
				'supply'=>$value['supply'],
				'dow_payments'=>$dow_payments['0']['dow_payments_title'],
				'pic'=>$pic[0],
		];
	}


	echo json_encode($arr);
}

//异步查询筛选条件
	public function shaixuan_all(){
			$db = Db::name('product');

			$dataname = $_POST['data_name'];
			$zidua = $_POST['zidua'];
					//优惠服务
						$data[$zidua] = $dataname;
						$list = $db->where($data)->field('id')->select();
						$ids = $this->selesid($list);
						$arr = array(
							'id'=>$ids,
						);
					 	$li = json_encode($arr);
						echo $li;
				// $arrd = [$id1.$id2];
				// $arr = array_unique($arrd);
				// $arr = array_values($arrd);
	}


	//查询id
	public function selesid($arr){
		$arrid = '';
		foreach ($arr as $k => $v) {
				$arrid .=$v['id'].',';
		}
		return $arrid;

	}
	//对应转换条件
	public function namez(){

				// 优惠
				$data[1] = '最低首付';
				$data[2] = '最低月供';
				$data[3] = '最高直降';
				$data[5] = '推荐';
				$data[6] = '热销';

				//用途
				$data[7] = '舒适性好';
				$data[8] = '入门代步';
				$data[9] = '硬派超野';
				$data[10] = '节能先锋';
				$data[11] = '居家旅行';
				$data[12] = '小钢炮';

				//变速箱
				$data[13] = '手动档';
				$data[14] = '自动档';


				// 能源
				$data[15] = '汽油';
				$data[16] = '油电混合';
				$data[17] = '新能源';


				//排量
				$data[18] = '1.0L以下';
				$data[19] = '1.0L-2.0';
				$data[20] = '2,0L-3.0L';
				$data[21] = '3.0L以上';


				//进气方式
				$data[22] = '涡轮增压';
				$data[33] = '自然吸气';


				// 驱动方式
				$data[23] = '前驱';
				$data[24] = '后驱';
				$data[25] = '四驱';

				//国别
				$data[26] = '中国';
				$data[27] = '德国';
				$data[28] = '美国';
				$data[29] = '法国';
				$data[30] = '日本';
				$data[31] = '韩国';
				$data[32] = '其他';

				// 车型
				$data[34] = '微型车';
				$data[35] = '小型车';
				$data[36] = '紧凑型车';
				$data[37] = '中大=型车';
				$data[38] = '中大型车';
				$data[39] = '大型车';
				$data[40] = '跑车';
				$data[41] = 'MPV';
				$data[42] = 'SUV';
				$data[43] = '微面';
				$data[44] = '微卡';
				$data[45] = '轻客';
				$data[46] = '皮卡';







				return $data;
	}
}
